<?php
namespace  app\manage\model;
use think\Db;
use think\Model;
class Role extends Model{
    protected  $table = 'role';
    /**
     * @param $page             页码
     * @param $rows             一页的列数
     * @param $keyword          关键字
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function onePage($page, $rows, $keyword, $create_id='') {
        $where = array(
            'name' => array('like', '%'.$keyword.'%')
        );
        if ($create_id != 1 && $create_id != '') {
            $where['create_id'] = $create_id;
        }
        $data=Db::name($this->table)
            ->where('name', 'like', '%'.$keyword.'%')
            ->where('status', 1)
            ->where($where)
            ->page($page, get_list_rows())
            ->field('role_id,name,is_platform,is_department,number')
            ->order('create_time desc')
            ->select();
        return $data;
    }

    /**
     * @param $keyword          关键字
     * @return int|string       总数
     * @throws \think\Exception
     */
    public function total($keyword, $create_id='') {
        $where = [];
        if ($create_id != 1 && $create_id != '') {
            $where['create_id'] = $create_id;
        }
        $data=Db::name($this->table)
            ->where('name', 'like', '%'.$keyword.'%')
            ->where('status', 1)
            ->where($where)
            ->count();
        if (gettype($data) !== 'integer') die;
        return $data;
    }

    /**
     * @param $data
     * @return int|string
     */
    public function m_insert($data) {
        $data = Db::name($this->table)
            ->insert($data);
        return Db::name($this->table)->getLastInsID();
    }

    /**
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_update($role_id, $data) {
        $data = Db::name($this->table)
            ->where(['role_id'=>$role_id])
            ->update($data);
        return $data;
    }

    /**
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_delete($role_id) {
        $data = Db::name($this->table)
            ->where(['role_id'=>$role_id])
            ->delete();
        return $data;
    }

    /**
     * @param $role_id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($role_id) {
        $data = Db::name($this->table)
            ->where(['role_id'=>$role_id])
            ->field('role_id, name, is_platform, is_department, number, create_time, update_time')
            ->find();
        return $data;
    }

    /**
     * 增加人数
     */
    public function add($role_id) {
        $data = Db::name($this->table)
            ->where(['role_id' => $role_id])->setInc('number', 1);
        return $data;
    }

    /**
     * 减少人数
     */
    public function del($department_id) {
        $data = Db::name($this->table)
            ->where(['role_id' => $department_id])->setInc('number', -1);
        return $data;
    }

    /**
     * @param $admin_id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRoleAdmin($admin_id) {
        $data = Db::name($this->table)
            ->where(['user_id'=>$admin_id])
            ->field('role_id, name, is_platform, is_department, number, create_time, update_time')
            ->find();
        return $data;
    }

    /**
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAll($admin_id='') {
        $where = [];
        if ($admin_id != 1 && $admin_id != '') {
            $where['create_id'] = $admin_id;
        }
        $data = Db::name($this->table)
            ->where('status', '<>', 3)
            ->where($where)
            ->field('role_id, name')
            ->select();
        return $data;
    }

    /**
     * 唯一性
     * @param $tel
     * @return int|string
     * @throws \think\Exception
     */
    public function uniqueness($name, $create_id) {
        $data = Db::name($this->table)
            ->where(['name'=>$name])
            ->where(['status'=>1])
            ->where('create_id', $create_id)
            ->count();
        return $data;
    }


    /**
     * 找到对应的平台账户的id
     * @param $admin_id
     */
    public function findPlatId($admin_id) {
        $data = Db::name('admin_user')
            ->where('admin_id', $admin_id)
            ->where('admin_status', 1)
            ->find();
        if ($data['admin_sign'] == 1) {
            return $admin_id;
        } else {
            return $data['create_id'];
        }
    }
}